<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
var tester = async_test('iframe');
function runTest() {
    var iframe = document.querySelector('iframe');
    var frame = frames[0];
    var sample = frame.document.body;
    var selection = frame.getSelection();
    selection.collapse(sample, 0);
    frame.document.execCommand('insertText', false, 'foo bar baz');

    // Set "bar" to |range|.
    var range = new Range(sample.document);
    range.setStart(sample.firstChild, 4);
    range.setEnd(sample.firstChild, 7);
    var rect = range.getBoundingClientRect();

    if (!window.internals && !window.eventSender)
        return;

    internals.settings.setEditingBehavior('win');
    internals.settings.setSmartInsertDeleteEnabled(true);
    internals.settings.setSelectTrailingWhitespaceEnabled(true);

    // Attempt to put the mouse over the word 'bar'.  The proper way to do this 
    // would be to put 'bar' inside an id'd span so that we'd know it's
    // coordinates exactly.
    var x = iframe.offsetLeft + rect.left + 3;
    var y = iframe.offsetTop + rect.top + 3
    eventSender.mouseMoveTo(x, y);

    // Select 'bar'
    eventSender.mouseDown();
    eventSender.mouseUp();
    eventSender.mouseDown();
    eventSender.mouseUp();

    // Pause so that the mouse down will start a drag instead of a triple click.
    eventSender.leapForward(1500);

    eventSender.mouseDown();
    eventSender.leapForward(1000);

    x += 100;
    // Drop it like it's hot.
    eventSender.mouseMoveTo(x, y);
    eventSender.mouseUp();

    tester.step(function() {
        assert_equals(sample.innerHTML, 'foo baz&nbsp;bar&nbsp;', 'innerHTML');
        assert_false(selection.isCollapsed, 'isCollapsed');
        assert_equals(selection.anchorNode, sample.firstChild, 'anchorNode');
        assert_equals(selection.anchorOffset, 7, 'anchorOffset');
    });

    tester.done();
}
</script>
<iframe srcdoc="<body contenteditable style='font-family:monospace'></body>" onload="runTest();"></iframe>
<p>This test uses the eventSender to do drag and drop. To run it manually, drag the word selected in the above subframe and drop it after 'baz'.  The result should be 'foo baz bar'.</p>
<div id="log"></div>
